﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QUANLIHOCSINH.BussinessObject;
using QUANLIHOCSINH.BussinessLogic;
namespace QUANLIHOCSINH
{
    enum Status_QuanLiNamHoc { View, Insert, Update, Delete };
    public partial class FrmQuanLiNamHoc : Form
    {
        Status_QuanLiNamHoc TrangThai = Status_QuanLiNamHoc.View;
        NAMHOCBUS namhoc = new NAMHOCBUS();
        public FrmQuanLiNamHoc()
        {
            InitializeComponent();
        }
        private void EnableButton(bool Enable)
        {
            btnThem.Visible = !Enable;
            btnSua.Visible = !Enable;
            btnXoa.Visible = !Enable;
            btnCapNhat.Visible = Enable;
            btnHuy.Visible = Enable;
            gbnamhoc.Enabled = Enable;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            txtMaNamHoc.Text = namhoc.NextID();
            EnableButton(true);
            btnThem.Visible = false ;
            TrangThai = Status_QuanLiNamHoc.Insert;
        }
    
        private void btnSua_Click(object sender, EventArgs e)
        {
            dgvNamHoc.ReadOnly = false;
            if (dgvNamHoc.Rows.Count > 1)
            {
                int r = dgvNamHoc.CurrentRow.Index;
                txtMaNamHoc.Text =dgvNamHoc.Rows[r].Cells["MaNamHoc"].Value.ToString();
                txtTenNamHoc.Text = dgvNamHoc.Rows[r].Cells["TenNamHoc"].Value.ToString();
                
            }
            EnableButton(true);
            btnSua.Visible = false;
            TrangThai = Status_QuanLiNamHoc.Update;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Bạn Có Muốn Xóa", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                NAMHOC nh = new NAMHOC();
                int r = dgvNamHoc.CurrentCell.RowIndex;
                nh.MaNamHoc = dgvNamHoc.Rows[r].Cells[0].Value.ToString();
                namhoc.XoaNamHoc(nh);
                FrmQuanLiNamHoc_Load(sender, e);
            }
        }
        private NAMHOC LayThongTinNamHoc()
        {
            NAMHOC nh = new NAMHOC();
            nh.MaNamHoc = txtMaNamHoc.Text;
            nh.TenNamHoc = txtTenNamHoc.Text;
            return nh;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
             
            if (TrangThai == Status_QuanLiNamHoc.Insert)
            {
                if (namhoc.ThemNamHoc(LayThongTinNamHoc()))
                {
                    FrmQuanLiNamHoc_Load(sender, e);
                }
            }
            if (TrangThai == Status_QuanLiNamHoc.Update)
            {
                if (namhoc.SuaNamHoc(LayThongTinNamHoc()))
                {
                    FrmQuanLiNamHoc_Load(sender, e);
                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            EnableButton(false);
        }

        private void FrmQuanLiNamHoc_Load(object sender, EventArgs e)
        {
            dgvNamHoc.DataSource = namhoc.LayDanhSachNamHoc();
            EnableButton(false);
        }

        private void lbTieuDeQuiDinh_Click(object sender, EventArgs e)
        {

        }
    }
}
